#!/usr/bin/python
# -*- coding: utf-8 -*-
'''
@author:atuo.cn
'''

from tkinter import *
from tkinter.ttk import *
import tkinter.messagebox as messagebox

from dialogs.sectiondialog_layout import SectionDialogLayout


class SectionDialog(SectionDialogLayout):
    
    def __init__(self, master, wmtitle, section):
        self.section=section
        SectionDialogLayout.__init__(self, master, wmtitle)

    def onLoad(self):
        self.title.set(self.section.title)
        self.rawtitle.set(self.section.rawTitle)
        
        text= "\n".join(self.section.text)
        self.textContent.insert(END,text)
        
    #override methods of Dialog
    def validate(self):
        b= bool(self.title.get())
        if not b:
            messagebox.showinfo(self.wmtitle, "请输入标题！")
        return b

    def apply(self):
        self.section.title=self.title.get().strip()
        if self.textContent.edit_modified():
            self.section.text=[t for t in self.getline(self.textContent) if t]
    
    def getline(self, textwidget):
        i=1
        while textwidget.compare("%d.0"%i, "<", END): 
            yield textwidget.get("%d.0"%i, "%d.end"%i)
            i+=1
                    
    #Event Handlers

